# import pytesseract

#
# # 打开图片
# img = Image.open(r'E:\auto\PythonProject1\testimage\a1.png')
# # 使用Tesseract进行文字识别
# text = pytesseract.image_to_string(img)
# print('print--->',text)
#
# import paddleocr
#
# # e:\Program Files\Tesseract-OCR
#
# # 创建一个OCR识别器
# ocr = paddleocr.ocr()
# # 读取图片
# img_path = r'E:\auto\PythonProject1\testimage\a1.png'
# img = paddleocr.read_image(img_path)
# # 进行文字识别
# result = ocr.ocr(img)
# # 显示识别结果
# for line in result:
#     print(' '.join((word_info[-1] for word_info in line)))


import easyocr
from PIL import Image
import uuid
# def initReader():
#     reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)  # this needs to run only once to load the model into memory
#     return reader


def generate_random_id():
    # 生成一个 UUID
    random_uuid = uuid.uuid4()
    # 将 UUID 转换为十六进制字符串并取前 16 位
    return random_uuid.hex[:16]

reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)  # this needs to run only once to load the model into memory

#img
def img2text(img):
    try:
        # reader = easyocr.Reader(['ch_sim','en'], gpu=False)
        # 緩存到本地
        imagepath=f'E:\\auto\\PythonProject1\\testimage\\{generate_random_id()}.png'
        img.save(imagepath)
        result = reader.readtext(imagepath, detail=0)
        # result = reader.readtext(r'E:\auto\PythonProject1\testimage\a1.png',detail = 0)
        print('orcinage result--->', result)
        return result
    except Exception as e:
         print('error orc',e)
         return None


# if __name__ == '__main__':
#     orcimage()